package com.tongfusoft.document.web.controller.administration;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tongfusoft.da.pagehelper.PageInfo;
import com.tongfusoft.document.entity.Privilege;
import com.tongfusoft.document.entity.RoleHasPrivileges;
import com.tongfusoft.document.manager.role.service.IRolePrivilegeService;
import com.tongfusoft.spring.web.core.controller.BaseController;
import com.tongfusoft.spring.web.core.controller.annotation.UrlDesc;
import com.tongfusoft.spring.web.core.data.DataTable;
import com.tongfusoft.spring.web.core.data.ResultModel;

@RequestMapping("/manager")
@Controller
public class RolePrivilegesController extends BaseController{
	@Autowired
	@Qualifier("rolePrivilegeService")
	private IRolePrivilegeService rolePrivilegeService;
	
	@UrlDesc(value = "角色权限管理页")
	@RequestMapping("/roleHasPrivileges")
	public String account(HttpServletRequest request, Model model) {

		model.addAttribute("mainPageKey", "roleHasPrivileges");
		model.addAttribute("mainPage", "manager/role/roleHasPrivileges");

		return "base";
	}
	
	@UrlDesc(value = "角色对应的权限列表")
	@RequestMapping("/role/privilegesByRole")
	@ResponseBody
	public DataTable privilegesByRole(Model model, int length, int start, int draw, String roleId){
		DataTable rm = new DataTable();
		try {
			int pageIndex = (start / length);;
			PageInfo<RoleHasPrivileges> pageInfo = this.rolePrivilegeService.privilegesListByRoleIdPageTable(pageIndex, 100, roleId);
			rm.setDraw(draw);
			rm.setRecordsFiltered(pageInfo.getTotal());
			rm.setRecordsTotal(pageInfo.getTotal());
			rm.setData(pageInfo.getList());
		}catch(Exception e){
			e.printStackTrace();
			super.logger.error(e);
		}
				
		return rm;
	}

	@UrlDesc(value = "角色对应的可选权限列表")
	@RequestMapping("/role/altPrivilegesByRole")
	@ResponseBody
	public ResultModel altPrivilegesByRole(String roleId){ 
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			rm.setData(this.rolePrivilegeService.altPrivilegesListByRoleId(roleId));
		}catch(Exception e){
			super.logger.error(e);
			rm.setState("error");
		}
			
		return rm;
	}

	@UrlDesc(value = "新增角色的权限")
	@RequestMapping("/role/addPrivileges")
	@ResponseBody
	public ResultModel addRolePrivileges(HttpServletRequest request,String roleId,String privileges){
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			this.rolePrivilegeService.addRolePrivileges(super.getUser(), roleId, Arrays.asList(privileges.split(",")));
		}catch(Exception e){
			super.logger.error(e);
			rm.setState("error");
			rm.setCode("add_roleprivilege_error");
		}
			
		return rm;
	}
	
	@UrlDesc(value = "删除角色的权限")
	@RequestMapping("/role/delPrivilege")
	@ResponseBody
	public ResultModel delRolePrivilege(HttpServletRequest request,String rpId){
		ResultModel rm = new ResultModel();
		rm.setState("success");
		try {
			this.rolePrivilegeService.delRolePrivilege(super.getUser(), rpId);
		}catch(Exception e){
			e.printStackTrace();
			super.logger.error(e);
			rm.setState("error");
			rm.setCode("del_roleprivilege_error");
		}
			
		return rm;
	}
	
}
